<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Common extends Controller{
	/**
	 * 自动执行的方法
	 */
	public function _initialize()
	{

		$route = strtolower(request()->module()."/".request()->controller()."/".request()->action());

		if(!$this->check(session('aid'),$route)){
			$this->error("你没有权限，请xxx 开通");
		}

		
	}

	public function check($aid,$route)
	{

		if(session('rule_list')){

			if(in_array($route,session('rule_list'))){
				return true;
			}else{
				return false;
			}
			
		}else{
			$roles = Db::name('auth_role')
	        ->alias('t1')
	        ->field('t1.rules')
	        ->join('auth_admin_role t2','t1.rid = t2.rid')
	        ->where('t2.aid',$aid)
	        ->where('t1.status','0')
	        ->select();

	        if(!$roles){
	        	return false;
	        }

	        $rules = [];

	        foreach ($roles as $key => $row) {
	        	$rules = array_merge($rules,explode(',',$row['rules']));
	        }

	        $rules = array_unique($rules);

	        
	        $data = Db::name('auth_rule')->where('ruid','in',$rules)->column('route');

	        session('rule_list',$data);

	        if(in_array($route,$data)){
	        	return true;
	        }else{
	        	return false;
	        }

		}
	}
}